1619C - Wrong Addition - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

t = int(input())
for i in range(t):
    ans = list()
    a, s = map(str, input().split())
    j = len(a) - 1
    k = len(s) - 1
    lis = ''
    while True:
        if k < j:
            ans = [-1]
            break
        if j == -1:
            lis = s[:k + 1]
            break
        if s[k] < a[j] and k > 0:
            if s[k - 1] == '1':
                s1 = int(s[k - 1] + s[k])
                k -= 1
                ans.append(s1 - int(a[j]))
            else:
                ans = [-1]
                break
        elif s[k] < a[j] and k == 0:
            ans = [-1]
            break
        else:
            ans.append(int(s[k]) - int(a[j]))
        k -= 1
        j -= 1
    if len(lis) == 0:
        while ans[-1] == 0 and len(ans) > 1:
            ans.pop()
    ans = reversed(ans)
    for i in ans:
        lis += str(i)
    print(lis)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){

   int t; cin>>t;
   while(t--){
      ll a,s; cin>>a>>s;
      bool flag=true;
      ll e=1,b=0;
      while(s>0){
        ll d=a%10,f=s%10;
        s/=10,a/=10;
        ll w=10;
        if(f<d){
            f=f+w*(s%10);
            s/=10,w*=10;
        }
        if(f-d>9 || f<d){
          flag=false;
          break;
        }
        b+=e*(f-d);
        e*=10;
      }
      if(a!=0){
        flag=false;
      }
      if(flag){
        cout<<b<<endl;
      }
      else{
        cout<<-1<<endl;
      }
   }

return 0;
}


Comments

Submit
0 Comments
More Questions

441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets